import Message from '@/util/message.js'
// import app from '@/main'
// import user from '@/api/user'
let server = 'https://api.kitakuarta.com'
// server = 'http://203.56.185.10:8211/admin-api'
export default {
    request(url, {
        method = 'GET',
        header = {},
        data = {},
        timeout = 30000,
        showLoading = true,
        showToast = true,
        needLogin = true,
    } = {}) {
        header['token'] = uni.getStorageSync('token')
		
		header['Access-Control-Allow-Origin'] = '*'

        if (!header['Content-Type']) {
            header['Content-Type'] = 'application/x-www-form-urlencoded'
        }
        return new Promise((resolve, reject) => {
            showLoading && Message.showLoading('处理中，请稍候')
            uni.request({
                url: `${server}${url}`,
                method,
                header,
                data,
                // withCredentials: true,
                timeout
            }).finally(() => {
                showLoading && Message.hideLoading()
            }).then(res => {
                const err = false
                const response = res
                if (err) {
                    showToast && Message.error("网络有问题，请稍后再试")
                    reject(err)
                } else if (response.statusCode === 200) {
                    // 后端接口状态码
                    if (response.data.code === 200) {
                        resolve(response.data)
                    } else {
                        showToast && Message.error(response.data.msg)
                        reject(response)
                    }
                } else if (response.statusCode === 401) {
                    uni.removeStorageSync('token')
                    // app.$store.dispatch("setLoginUserFun", {})
                    const pages = getCurrentPages()
                    const path = pages[pages.length - 1].route
                    if(needLogin){
                        // user.login('/' + path)
                    }
                    // showToast && Message.error('登录信息已失效')
                    reject(response)
                } else {
                    showToast && Message.error("请求失败: " + response.statusCode)
                    reject(response)
                }
            })
        })
    },
    postForm(url, data) {
        return this.request(url, {
            method: 'POST',
            data
        })
    },
    postJson(url, data) {
        return this.request(url, {
            method: 'POST',
            header: {
                'Content-Type': 'application/json'
            },
            data
        })
    },
    postJsonConf(url, data, conf) {
        return this.request(url, {
            method: 'POST',
            header: {
                'Content-Type': 'application/json'
            },
            data,
            ...conf
        })
    },
    postJsonTimeout(url, data, timeout) {
        return this.request(url, {
            method: 'POST',
            header: {
                'Content-Type': 'application/json'
            },
            data,
            timeout
        })
    },
    get(url, data,conf = {}) {
        return this.request(url, {
            data,
			...conf
        })
    },
    uploadFile(filePath) {
        return new Promise((resolve, reject) => {
            Message.showLoading('处理中，请稍候')
            uni.uploadFile({
                url: `${server}/common/file/upload`,
                filePath: filePath,
                name: 'files',
                success: function (res) {
                    if (res.statusCode !== 200) {
                        Message.error("请求失败：" + res.statusCode)
                        reject(res)
                    } else {
                        const data = JSON.parse(res.data)
                        if (data.code !== 200) {
                            Message.error(data.msg || '操作失败')
                            reject(data.msg || '请求失败')
                        } else {
                            resolve(data)
                        }
                    }
                },
                fail: function (err) {
                    reject(err)
                },
                complete: function () {
                    Message.hideLoading()
                }
            })
        })
    },
}